package cn.smdq.ch.upms.admin.pay.channel.support;

import cn.smdq.ch.upms.admin.pay.channel.PayChannel;
import cn.smdq.ch.upms.admin.pay.channel.param.AliPayRequestParam;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.util.Assert;

import static org.springframework.util.StringUtils.isEmpty;

/**
 * @author shuimodanqing
 * create at:  2019/1/29  2:01 PM
 * @description 支付宝支付父类
 */
public abstract class BaseAliPayChannel implements PayChannel {

    protected AlipayClient alipayClient;

    protected AliPayRequestParam payRequestParamToUse;

    public BaseAliPayChannel(JSONObject requestParam) {
        this.payRequestParamToUse = resolveParamForAliPay(requestParam);
        alipayClient = DefaultAlipayClient.builder(payRequestParamToUse.getGateWay(), payRequestParamToUse.getAppId(), payRequestParamToUse.getPrivateKey())
                .alipayPublicKey(payRequestParamToUse.getAliPublicKey())
                .charset(payRequestParamToUse.getCharset())
                .format(payRequestParamToUse.getFormat())
                .signType(payRequestParamToUse.getSignType()).build();
    }


    private AliPayRequestParam resolveParamForAliPay(JSONObject requestParam) {
        //一.请求参数的封装

        //1.应用appId(支付宝分配给开发者的应用ID)
        String appId = requestParam.getString("appId");
        Assert.notNull(appId, "appId si not null!");

        //2.支付宝网关(请求支付宝进行处理的接口)
        String gateWay = requestParam.getString("gateWay");
        Assert.notNull(gateWay, "gateWay is not null!");

        //3.个人私钥(加签使用)
        String privateKey = requestParam.getString("privateKey");
        Assert.notNull(privateKey, "privateKey si not null!");

        //4.仅支持JSON
        String format = requestParam.getString("format");
        format = isEmpty(format) ? "JSON" : format;
        //5.编码集
        String charset = requestParam.getString("charset");
        charset = isEmpty(charset) ? "utf-8" : charset;

        //6.支付宝公钥
        String aliPublicKey = requestParam.getString("publicKey");
        Assert.notNull(aliPublicKey, "支付宝公钥不能为空 for param aliPublicKey");

        //7.签名类型
        String signType = requestParam.getString("signType");
        Assert.notNull(signType, "签名类型不能为空: signType");

        //二.请求model的封装(具体的商品信息的描述)
        String body = requestParam.getString("body");
        Assert.notNull(body, "body is not null!");

        String notifyUrl = requestParam.getString("notifyUrl");

        String subject = body;

        String outTradeNo = requestParam.getString("outTradeNo");
        Assert.notNull(outTradeNo, "outTradeNo is not null!");

        String timeOutExpress = requestParam.getString("timeOutExpress");

        String timeExpire = requestParam.getString("timeExpire");

        String totalAmount = requestParam.getString("totalFee");
        Assert.notNull(totalAmount, "支付金额不能为空");

        String productCode = "QUICK_MSECURITY_PAY";

        String goodsType = "1";

        return new AliPayRequestParam.Builder()
                .appId(appId).aliPublicKey(aliPublicKey).body(body).format(format).goodsType(goodsType).notifyUrl(notifyUrl).charset(charset)
                .outTradeNo(outTradeNo).privateKey(privateKey).productCode(productCode).signType(signType).subject(subject)
                .timeExpire(timeExpire).timeoutExpress(timeOutExpress).gateWay(gateWay)
                .totalFee(totalAmount)
                .build();

    }

}
